## extract gtf file that overlapped
## author: Yaping Liu  lyping1986@gmail.com

my $file_name=$ARGV[0];
#my $word1=$ARGV[1]; #gch
#my $word2=$ARGV[2]; #gcg

my $use_age = "USAGE: perl distance_distribution_between_elements.pl reference_file_name";
if($#ARGV < 0){
	print "$use_age\n";
	exit(1);
}


my $distance_pre_gch = 0;
my $distance_post_gch = 0;

my $pos_pre_gch = 0;
my $pos_post_gch = 0;
my $pos_gcg = 0;

my $distacne_to_gch;
my $gcg_count=0;
my $gch_count=0;

my $count=0;

my $histogram_file_name = $file_name.".chr1.fwd.hist.txt";
open(OUT,">$histogram_file_name");

my $flag_chr1=0;

my @data_tmp=('n','n','n','n');
open(FH,"<$file_name") or die;
while(<FH>){
	my $line = $_;
	chomp($line);

	#next if $line =~ /chr\S+_\S+/;
	if($line=~/(chr\S+)/){
		print "$line\n";
		$count=0;
		if($line=~/(chr1$)/){
			$flag_chr1=1;
		}
		else{
			exit(1);
		}
		
		next;
	}
	else{
		my @splitline=split "",$line;
		$data_tmp[2] = $splitline[0];
		$data_tmp[3] = $splitline[1];
		&check(@data_tmp);
		&check(@splitline);
		$data_tmp[0] = $splitline[$#splitline-1];
		$data_tmp[1] = $splitline[$#splitline];
	}

}
close(OUT);

sub check(){
	my @splitline = @_;
	my $printLine = join "",@splitline;
	
	for(my $i=0;$i<=$#splitline-2;$i++){
		$count++;
	#	if($flag_post_gch == 1){
	#		$distance_post_gch++;
	#	}
	#	if($flag_pre_gch == 1){
	#		$distance_pre_gch++;
	#	}
		if($splitline[$i] eq 'n' or $splitline[$i] eq 'N'){
			next;
		}
		my @triple_to_check=($splitline[$i],$splitline[$i+1],$splitline[$i+2]);
		my $check_gcg = &is_gcg(@triple_to_check);
		if($check_gcg == 1){
			if($pos_gcg > $pos_post_gch && $pos_post_gch != 0){ ## meet the second gcg, but not the >=third continuous gcg
				$distance_pre_gch = $pos_gcg - $pos_post_gch;
				print OUT "$i\t$pos_pre_gch\t$pos_gcg\t$pos_post_gch\t$distance_pre_gch\t$printLine\n";
				#print OUT "$distance_pre_gch\n";
				$pos_post_gch = 0;
				$pos_pre_gch = 0;
			}
			$pos_gcg = $count;
			
			next;
		}
		my $check_gch = &is_gch(@triple_to_check);
		
		if($check_gch == 1){
			
			$pos_pre_gch = $pos_post_gch;
			$pos_post_gch = $count;
			if($pos_post_gch > $pos_gcg && $pos_pre_gch > $pos_gcg && $pos_gcg != 0){ ##avoid two continuous gch
				
				$pos_gcg=0;
			}
			elsif($pos_pre_gch < $pos_gcg && $pos_post_gch > $pos_gcg && $pos_gcg != 0){
				
				$distance_post_gch =  $pos_post_gch - $pos_gcg;
				$distance_pre_gch =  $pos_gcg - $pos_pre_gch;
				if($distance_post_gch < $distance_pre_gch){
					print OUT "$i\t$pos_pre_gch\t$pos_gcg\t$pos_post_gch\t$distance_post_gch\t$printLine\n";
					#print OUT "$distance_post_gch\n";
				}
				else{
					print OUT "$i\t$pos_pre_gch\t$pos_gcg\t$pos_post_gch\t$distance_pre_gch\t$printLine\n";
					#print OUT "$distance_pre_gch\n";
				}
			}
	
			next;
		}
	}
}

sub is_gch(){
	my @splitline = @_;
	if(($splitline[0] eq 'G' or $splitline[0] eq 'g') and ($splitline[1] eq 'C' or $splitline[1] eq 'c')){ #forward
			if($splitline[2] ne 'G' or $splitline[2] ne 'g'){
					return 1;
			}

	}
#	if(($splitline[2] eq 'C' or $splitline[2] eq 'c') and ($splitline[1] eq 'G' or $splitline[1] eq 'g')){ #reverse
#			if($splitline[0] eq 'C' or $splitline[0] eq 'c'){

#			}
#			else{
#				return 1;
#			}
#	}
	
	return 0;
}

sub is_gcg(){
	my @splitline = @_;
	
	if(($splitline[2] eq 'G' or $splitline[2] eq 'g') and ($splitline[1] eq 'C' or $splitline[1] eq 'c')){ #forward
			if($splitline[0] eq 'G' or $splitline[0] eq 'g'){

				return 1;
			}
			
	}
#	if(($splitline[2] eq 'C' or $splitline[2] eq 'c') and ($splitline[1] eq 'G' or $splitline[1] eq 'g')){ #reverse
#			if($splitline[0] eq 'C' or $splitline[0] eq 'c'){
#				return 1;
#			}
			
#	}
	return 0;
}

